package com.bocloud.cmp.dao.app.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.app.DeployHistoryDao;
import com.bocloud.cmp.entity.app.resource.DeployHistory;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 
 * @author zjm
 * @date 2017年4月4日
 */
@Repository("deployHistoryDao")
public class DeployHistoryDaoImpl extends JdbcGenericDao<DeployHistory, Long> implements DeployHistoryDao {

	@Override
	public DeployHistory query(Long id) throws Exception {
		String sql = "select cu.name as creator_name, mu.name as mender_name, a.name as app_name, dh.* "
				+ "from deploy_history dh " + "LEFT JOIN `user` cu on cu.id = dh.creater_id "
				+ "LEFT JOIN `user` mu on mu.id = dh.mender_id " + "LEFT JOIN `application` a on a.id = dh.app_id "
				+ "where dh.is_deleted = 0 and dh.id = :id  group by dh.id ";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<DeployHistory> list = this.list(DeployHistory.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<DeployHistory> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select cu.name as creator_name, mu.name as mender_name, a.name as app_name, dh.* "
				+ "from deploy_history dh " + "LEFT JOIN `user` cu on cu.id = dh.creater_id "
				+ "LEFT JOIN `user` mu on mu.id = dh.mender_id " + "LEFT JOIN `application` a on a.id = dh.app_id "
				+ "where dh.is_deleted = 0 ";
		sql = SQLHelper.buildRawSql(sql, page, rows, params, sorter, "dh");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.list(DeployHistory.class, sql, paramMap);
	}

	@Override
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select dh.id, dh.name from deploy_history dh where dh.is_deleted = 0 ";
		sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "dh");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<DeployHistory> deployHistorys = this.list(DeployHistory.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (DeployHistory deployHistory : deployHistorys) {
			beans.add(new SimpleBean(deployHistory.getId(), deployHistory.getName()));
		}
		return beans;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from deploy_history dh " + "LEFT JOIN `user` cu on cu.id = dh.creater_id "
				+ "LEFT JOIN `user` mu on mu.id = dh.mender_id " + "LEFT JOIN `application` a on a.id = dh.app_id "
				+ "where dh.is_deleted = 0 ";
		sql = SQLHelper.buildRawSql(sql, params, null, "dh");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public boolean deleteById(Long id, Long userId) throws Exception {
		String sql = "update deploy_history "
				+ "set is_deleted = true , gmt_modify = :gmtModify , mender_id = :menderId "
				+ "where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}
	
	public boolean deleteByResourceName(String resouorceName, Long userId) throws Exception {
		String sql = "update deploy_history "
				+ "set is_deleted = true , gmt_modify = :gmtModify , mender_id = :menderId "
				+ "where is_deleted = 0 and resource_name = :resouorceName";
		Map<String, Object> params = MapTools.simpleMap("resouorceName", resouorceName);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	@Override
	public DeployHistory getByResourceNameAndImage(String resourceName, String currentVersion) throws Exception {
		String sql = "select * from deploy_history where is_deleted = 0 "
				+ "and status = 'SUCCESS' and resource_name = :resourceName "
				+ "and current_version = :currentVersion "
				+ "order by gmt_modify desc limit 1";
		Map<String, Object> params = MapTools.simpleMap("resourceName", resourceName);
		params.put("currentVersion", currentVersion);
		List<DeployHistory> list = this.list(DeployHistory.class, sql, params);
		if (ListTool.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

}
